﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Build.Utilities;

namespace CustomTasks
{
    public class StripComments : Task
    {
        public string InputFile { get; set; }
        public string OutputFile { get; set; }

        public override bool Execute()
        {
            var newConfig = new StringBuilder();

            try
            {
                List<string> file;

                var validator = new Validator();
                if (!validator.Validate(InputFile, OutputFile, out file))
                    return false;

                bool isMultiRowComment = false;

                foreach (string line in file)
                {
                    //Comment row
                    if (line.BeginsWithStartTag() && line.EndsWithEndTag())
                    {
                        continue;
                    }

                    //Row with initial comment
                    if (line.BeginsWithStartTag() && line.HasEndTag())
                    {
                        newConfig.AppendLine(line.RemoveStartComment());
                        continue;
                    }

                    //Row with comment att ebd of line
                    if (line.HasStartTag() && line.EndsWithEndTag())
                    {
                        newConfig.AppendLine(line.RemoveEndComment());
                        continue;
                    }

                    //Multirow comment start
                    if (line.BeginsWithStartTag() && !line.HasEndTag())
                    {
                        //Set to true so we skip rows without comment tags until end found
                        isMultiRowComment = true;
                        continue;
                    }

                    //Multirow end
                    if (!line.HasStartTag() && line.HasEndTag())
                    {
                        //Set to false so we scan normally again
                        isMultiRowComment = false;
                        continue;
                    }

                    //Check if in multirow comment, the continue to next row
                    if (isMultiRowComment)
                    {
                        continue;
                    }

                    //No tags or multrow comment, just plain config row.
                    newConfig.AppendLine(line);
                }

                SaveFile(newConfig, OutputFile);
            }
            catch (ApplicationException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        private static void SaveFile(StringBuilder newConfig, string outputFileName)
        {
            if (File.Exists(outputFileName)) File.Delete(outputFileName);

            File.WriteAllText(outputFileName, newConfig.ToString());
        }
    }
}